/*
 * ThreadHandler.h
 *
 *  Created on: 2012-11-30
 *      Author: yangentao@gmail.com
 */

#pragma once;

#include <boost/signals2.hpp>
#include <list>
#include <windows.h>
#include <exception>
#include "AutoLock.h"
#include "Thread.h"
#include "Event.h"
#include "String.h"
#include "MessageQueue.h"

using namespace std;

namespace yet {


//TODO postDelay
class Handler : public ThreadLock{
protected:
	typedef boost::signals2::signal<void(Message& msg)> MsgSignal;
	typedef MsgSignal::slot_type MsgHandler;
public:
	Handler();
	virtual ~Handler();
public:
	boost::signals2::connection addHandler(const MsgHandler& msgHandler);
	void post(Message msg, bool front = false);
	void postFront(Message msg);
	//shutdown when the previous message processed
	void postQuitMessage();
	bool start();
	//shutdown immediatly
	void quit();
	bool isActive();
protected:
	void peekAndProcess();
public:
	virtual DWORD waitFinish();
protected:
	virtual void notify() = 0;
	virtual bool onStart() = 0;
	virtual void onQuit() = 0;
public:
	boost::function<void(Message)> OnMessage;
	boost::function<void(Message)> OnCancel;
protected:
	MessageQueue messages;
private:
	MsgSignal sig;
	bool volatile active;
};





}
